package com.example.dogliving.fragment.column;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.example.dogliving.R;
import com.example.dogliving.columnmodle.IColumnCLassView;
import com.example.dogliving.helper.SpacesItemDecoration;
import com.example.dogliving.presenter.ColumnPresenter;

public class ColumnFragment extends Fragment implements IColumnCLassView{
    private RecyclerView recyclerView;
    private Context mContext;
    private ColumnPresenter columnPresenter;
    private int tage=0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //加载页面
        View view=inflater.inflate(R.layout.fragment_column,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView= (RecyclerView) view.findViewById(R.id.fragment_column);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initDataMore();
    }

    private void initDataMore() {
        //横向一行3个item
        GridLayoutManager mGmanager = new GridLayoutManager(mContext,3);
        //每个item间距10dp
        recyclerView.addItemDecoration(new SpacesItemDecoration(12));
        //绑定mannager
        recyclerView.setLayoutManager(mGmanager);
        //与P层交互
        columnPresenter = new ColumnPresenter(this);
        columnPresenter.login(mContext , recyclerView);

    }
    //绑定适配器
    public void toSetAdapter(ColumnAdapter adapter){
        if (adapter != null){
            recyclerView.setAdapter(adapter);
        }
    }
    //如果出现错误，吐司
    public void showError(Exception e){
        Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show();
    }

}
